<?php
/**
 *      [Jsen.net] (C)2013-2099 Jason Group Science and technology co., LTD.
 *      This is NOT a freeware, use is subject to license terms
 *
 *      http://www.jsen.net
 *      tel:400-681-9511
 */
libfile('pay_abstract');
class ecpay extends pay_abstract {
	public function __construct($config = array()) {	
		if (!empty($config)) $this->set_config($config);        
		$this->config['gateway_url'] = 'https://user.ecpay.cn/paygate.html';
		$this->config['gateway_method'] = 'POST';
		$this->config['notify_url'] = return_url('ecpay', 'notify');
		$this->config['return_url'] = return_url('ecpay', 'return');
	}

	public function getpreparedata() {
		$prepare_data['merId'] = $this->config['account'];
		$prepare_data['dealName'] =  "账户充值";
		$prepare_data['dealOrder'] = $this->product_info['trade_sn'];
		$prepare_data['dealFee'] =$this->product_info['total_fee'];
		$prepare_data['dealBank'] ="" ;             
		$prepare_data['dealReturn'] = $this->config['return_url'];
		$prepare_data['dealNotify'] = $this->config['notify_url'];		
		$prepare_data['dealSignure'] =$this->getSign( "ecpay", $prepare_data);
		if(get_client_ip()!="218.56.49.120"){
			//exit;
		}
		return $prepare_data;
	}

	public function _delivery() {
		return TRUE;
	}
    
    public function _return() {
		//订单号
		$BillNo = $_REQUEST["dealOrder"];
		//金额
		$Amount = $_REQUEST["dealState"];
		//校验源字符串
	  	$md5src = $BillNo.$Amount;
	    //MD5检验结果
		$md5sign = sha1($md5src);
		$dealState= $_REQUEST["dealState"];
		//$_REQUEST["BillNo"]="cz2015111617395809503";
        if ($dealState=="SUCCESS"){
			$result['result'] = 'success';
            $result['pay_code'] = 'ecpay';
            $result['trade_no'] = $_REQUEST['dealOrder'];
            $result['out_trade_no'] = $_REQUEST['dealOrder'];
            return $result;
        } else {
            return false;
        }
    }
    /**
	 * POST接收数据
	 * 状态码说明  （0 交易完成 1 待付款 2 待发货 3 待收货 4 交易关闭 5交易取消
	 */
    public function _notify() {
        return $this->_return();
    }
	
    private function getSign($type,$data){
        $signarray=array('merId','dealOrder','dealFee','dealReturn');//校验源字符串
		foreach($signarray as $v){
			if(!isset($data[$v])) $md5str .= "";
			else $md5str .= "$data[$v]";
		}
		$md5str.=$this->config['partner'];//MD5密钥
		$md5str = sha1($md5str);
		return $md5str;
    }
		
    /**
     * 相应服务器应答状态
     * @param $result
     */
    public function response($result) {
    	if (FALSE == $result) echo '';
		else echo 'success';
    }
    
    /**
     * 返回字符过滤
     * @param $parameter
     */
    private function filterParameter($parameter)
    {
        $para = array();
        foreach ($parameter as $key => $value) {
            if ('sign' == $key || 'sign_type' == $key || '' == $value || 'm' == $key  || 'a' == $key  || 'c' == $key   || 'code' == $key || 'method' == $key || 'page' == $key) continue;
            else $para[$key] = $value;
        }
        return $para;
    }
    public function getCodeUrl(){
    	
    }
}